<?php
/**
 * Created by PhpStorm.
 * User: 蒙明然
 * Date: 2017/9/4
 * Time: 9:28
 */

namespace app\admin\controller;
use think\Cache;
use think\Controller;
use think\config;
use app\common\model\Equipments as EquipmentModel;

class Equipments extends Base
{
    /**
     * 设备分类模版渲染
     */
    public function index(){
        Cache::clear();
        $key = input('key');//搜索条件
        $map = [];
        if($key&&$key!==""){
            $map['equipments_name'] = ['like',"%" . $key . "%"];
        }
        $group = new EquipmentModel();
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('pagesize');
        $count = $group->getAllCount($map);         //获取总条数
        $allpage = intval(ceil($count / $limits));  //计算总页面
        $lists = $group->getAll($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('vals', $key);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    /**
     * 添加设备
     */
    public function addEquipment(){
        Cache::clear();
        $m_equipment = new EquipmentModel();
        if(request()->isPost()){
            $param = $this->request->post();
            $res = $m_equipment->insertOne($param);
            if($res){
                return json(['code'=>1,'msg'=>'添加成功','data'=>'']);
            }else{
                return json(['code'=>2,'msg'=>'添加失败','data'=>'']);
            }
        }
        return $this->fetch();
    }
    /**
     * 编辑设备
     */
    public function update(){
        Cache::clear();
        $m_equipment = new EquipmentModel();
        $param = $this->request->post();
        $id = input("param.id");
        $where['equipments_id']=$id;
        //查询用户信息
        $data = $m_equipment->selectOne($where);
        $this->assign('res',$data);
        return $this->fetch();
    }
    public function doupdate(){
        Cache::clear();
        $m_equipment = new EquipmentModel();
        if(request()->isPost()){
            $data = $this->request->post();
            $where['equipments_id']=$data['equipments_id'];
            $res = $m_equipment->updateOne($where,$data);
            if($res){
                return $this->success('更新成功！','Equipments/index');
            }else{
                return $this->error('更新失败！','Equipments/index');
            }
        }
    }
    /**
     * 删除设备
     */
    public function del(){
        $m_equipment = new EquipmentModel();
        $id = input('param.id');
        $res = $m_equipment->deleteOne($id);
        if($res){
            return json(['code'=>1,'msg'=>'删除成功','data'=>'']);
        }else{
            return json(['code'=>1,'msg'=>'删除失败','data'=>'']);
        }
    }

}